// components/sms-input/sms-input.js
import {getSms} from "../../service/User";
import {wxLogin} from "../../service/Wx";
import Notify from "@vant/weapp/notify/notify";
import {verifyInput, verifyPhone} from  "../../utils/Util";
import Dialog from "@vant/weapp/dialog/dialog";
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        tel: {  //发送的手机号
            type: String,
            value: ""
        },
        type: {  //login // register 登录注册
            type: String,
            value: ""
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        _sendBtn: {
            times: 60,  //定时器 秒单位
            text: "获取验证码",
            loading: false,
            disabled: false
        }
    },
    _timeInterval: null, //自定义定时器

    /*
    * 组件生命周期
    * */
    lifetimes: {
        detached: function () {
            clearInterval(this._timeInterval)
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        onInputChange: function (e) {
            this.triggerEvent("verificationCodeInput", e.detail, {});
        },
        bindSendSms: async function () { //发送验证码
            try {
                const that = this;
                let times = that.data._sendBtn.times;
                if (!that._checked()) {return false} //数据校验
                that.setData({
                    "_sendBtn.disabled": true,
                    "_sendBtn.loading": true
                });
                let code = await wxLogin();
                await getSms({type: that.data.type, tel: that.data.tel, code})
                that.setData({
                    "_sendBtn.loading": false,
                    "_sendBtn.text": "重新发送(" + times + "s)"
                });
                that._timeInterval = setInterval(function () {
                    if (times <= 0) {
                        that.setData({
                            "_sendBtn.disabled": false,
                            "_sendBtn.text": "获取验证码"
                        });
                        clearInterval(that._timeInterval);
                    } else {
                        times --
                        that.setData({
                            "_sendBtn.text": "重新发送(" + times + "s)"
                        });
                    }
                }, 1000)
            } catch (e) {
                this.setData({
                    "_sendBtn.disabled": false,
                    "_sendBtn.loading": false
                });
                e && e.errMsg ? await Dialog.alert({
                    title: "提示",
                    message: e.errMsg || "",
                    context: this,
                    selector: "#van-dialog_sms"
                }) : null
                console.error(e)
            }
        },

        // 合法校验
        _checked: function () {
            let type = this.data.type;
            let tel = this.data.tel;
            if (!type) {
                Notify({
                    context: this,
                    selector: "#van-notify_sms",
                    type: "warning",
                    message: "验证码类型错误"
                });
                return false
            } else if (!verifyInput(tel)) {
                Notify({
                    context: this,
                    selector: "#van-notify_sms",
                    type: "warning",
                    message: "手机号不能为空"
                });
                return false
            } else if (!verifyPhone(tel)) {
                Notify({
                    context: this,
                    selector: "#van-notify_sms",
                    type: "warning",
                    message: "手机号格式不正确"
                });
                return false
            } else {
                return true
            }
        }
    }
})
